var loaderUtils = require("loader-utils");

function imgLoader(buffer) {
    const { limit, filename } = this.query;
    var content;
    if (buffer.byteLength > limit) {
        content = getBase64(buffer); 
    } else {
        var filePath = getFilePath.call(this, buffer, filename);
        this.emitFile(filePath, buffer);
        content = filePath;
    }
    return `module.exports = \`${content}\``;
}

imgLoader.raw = true; // 该loader要处理的是原始数据

module.exports = imgLoader;

function getBase64(buffer) {
    return "data:image/jpg;base64,"+buffer.toString('base64');
}

function getFilePath(buffer, name) {
    return loaderUtils.interpolateName(this, name, {
        content: buffer
    })
}